#coding:utf-8
from django.db.models import Q
from django.db.models import Count

from lovewith.utils.mtcache import MtCache
from lovewith.utils.tools import my_iterator
from lovewith.share.models import AttachTag, MtPresetTag


#标签
class AttachTagApi:
    def __init__(self):
        pass

    @staticmethod
    def check_data(tag):
        tag_data = AttachTag.objects.filter(tagname=tag)
        if tag_data.exists():
            return tag_data[0]
        else:
            return False

    #热门标签
    @staticmethod
    def get_hot(count, cate_id):
        total = int(count)
        if not cate_id:
            cate_id = 'all'

        hot_tag_data = MtCache('hot_tag').get(cate_id=cate_id)

        if not hot_tag_data:
            hot_tag_data = []
            if cate_id == 'all':
                tag_data = AttachTag.objects.values('tagname').annotate(dcount=Count('tagname'))
                tag_data = tag_data.order_by('-dcount')[0: total]
            else:
                tag_data = AttachTag.objects.filter(cate_id=cate_id).values('tagname').annotate(
                    dcount=Count('tagname')).order_by('-dcount')[0: total]

            for tag in my_iterator(tag_data):
                hot_tag_data.append(tag)

            MtCache('hot_tag').set(hot_tag_data, cate_id=cate_id)
        return hot_tag_data


    #获取图片tag
    @staticmethod
    def get_by_image(image_id):
        result = []
        tag_data = AttachTag.objects.filter(post_image_id=int(image_id))

        for tag in tag_data.iterator():
            result.append(tag.tagname)

        return result

    #tag
    @staticmethod
    def search(tag, start, last):
        #单个关键词搜索
        return AttachTag.objects.filter(post_image__share_post__is_delete=False, tagname__icontains=tag).order_by('-id')[start: last]


    #统计搜索结果记录总数
    @staticmethod
    def get_search_count(tag):
        return AttachTag.objects.filter(tagname__icontains=tag).count()

    #删除
    @staticmethod
    def delete_by_image(image_id):
        img_color = AttachTag.objects.filter(post_image_id=image_id)
        img_color.delete()

    #add
    @staticmethod
    def add(tags, image_data, cate_data):
        new_tags = []
        for t in tags:
            if t != '':
                new_tags.append(
                    AttachTag(tagname=t, post_image=image_data, cate=cate_data)
                )
        AttachTag.objects.bulk_create(new_tags)

    #获取用户订阅内容总数
    @staticmethod
    def get_user_subscribe_count(sub_data):
        #筛选出分类
        q_object = Q()
        for sub in sub_data:
            if sub.get('cate_id'):
                q_object.add(Q(cate_id=sub.get('cate_id')), q_object.OR)
            else:
                q_object.add(Q(tagname__icontains=sub.get('keyword')), q_object.OR)

        return AttachTag.objects.filter(q_object).values_list('post_image').distinct().order_by('-post_image').count()

    #获取用户订阅内容
    @staticmethod
    def get_user_subscribe(sub_data, start, last):
        #筛选出分类
        q_object = Q()
        for sub in sub_data:
            if sub.get('cate_id'):
                q_object.add(Q(cate_id=sub.get('cate_id')), q_object.OR)
            else:
                q_object.add(Q(tagname__icontains=sub.get('keyword')), q_object.OR)

        return AttachTag.objects.filter(q_object).filter(post_image__share_post__is_delete=False).values_list(
            'post_image',
            'post_image__path',
            'post_image__share_post'
        ).distinct().order_by('-post_image')[start: last]

    #获取预设tag
    @staticmethod
    def get_preset(tag):
        result = []
        preset_data = MtPresetTag.objects.filter(tag__icontains=tag)
        for preset in preset_data:
            #替换中文逗号
            tag = preset.tag_related.replace('，', ',').split(',')
            result.extend(tag)

        return result
